<?php


namespace app\admin\controller;

use app\common\model\mysql\AdminTaxon;
use app\BaseController;
use think\App;
use think\facade\{View};
use think\auth\Auth;
class Classify extends BaseController
{
    private $model ;
    private $auth;
    public function __construct()
    {
        $this->model = new AdminTaxon;
        $this->auth = Auth::instance();
    }

    //分类管理页
    public function multistage()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])){
            return View::fetch('Classify/cate');
        }
    }

    //渲染权限表数据
    public function getRules()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])) {
            $id = request()->param('id');
            $demo = AdminTaxon::getAttrAllMessage($id);
            return json($demo);
        }
    }

    //是否开启该规则
    public function updateRulesState()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])) {
            //是否为post请求访问
            if (!request()->isPost()) {
                return show(config("status.error"), '请求方式错误');
            }

            $id = request()->param('id');
            $field = request()->param('field');
            $value = request()->param('value');
            return $this->model->updateRulesState($id, $field, $value);
        }
    }

    //添加子规则
    public function saveRules()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])) {
            //是否为post请求访问
            if (!request()->isPost()) {
                return show(config("status.error"), '请求方式错误');
            }

            $rules = [
                'pid' => input('pid'),
                'name' => input('name'),
                'title' => input('title'),
                'condition' => input('condition'),
                'remarks' => input('remarks'),
                'last_ip' => request()->ip(),
                'last_time' => time()
            ];
            return json($this->model->saveRules($rules));
        }
    }

    //删除该规则
    public function delRules()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])) {
            //是否为post请求访问
            if (!request()->isPost()) {
                return show(config("status.error"), '请求方式错误');
            }

            $id = request()->param('id');
            return json($this->model->delRules($id));
        }
    }

    //修改该规则
    public function updateRules()
    {
        if ($this->auth->check('Classify/multistage',session(config("session.session_admin"))['id'])) {
            $rules = [
                'id' => input('id'),
                'pid' => input('pid'),
                'name' => input('name'),
                'title' => input('title'),
                'condition' => input('condition'),
                'remarks' => input('remarks'),
                'navid' => input('navid'),
                'last_ip' => request()->ip(),
                'last_time' => time()
            ];
            return json($this->model->updateRules($rules));
        }
    }
}